package com.fenmirs.bookmarks.utils;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * @author fenmirs
 * @create 2019-04-02 9:33
 * 10位 时间操作类
 */
public class TenTime {
    /**实例*/
    public TenTime() {
        time = LocalDateTime.now();
    }
    /**秒级时间戳实例*/
    public TenTime(long timestamp) {
        time = LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp),  ZoneId.systemDefault());
    }
    /**日期实例*/
    public TenTime(Date date) {
        time = LocalDateTime.ofInstant(date.toInstant(),ZoneId.systemDefault());
    }
    /**字符串实例*/
    public TenTime(String timeStr, String pattern){
        if(patternSupport(pattern)){
            try {
                SimpleDateFormat sf = new SimpleDateFormat(pattern);
                Date date = sf.parse(timeStr);
                time = LocalDateTime.ofInstant(date.toInstant(),ZoneId.systemDefault());
            }catch (Exception e){
                throw new RuntimeException("不支持此时间格式");
            }
        }else{
            throw new RuntimeException("不支持此时间格式");
        }
    }


    /**大中华时区*/
    private static final ZoneOffset chinaZone = ZoneOffset.of("+8");
    /**基准时间*/
    private LocalDateTime time;
    /**时间格式*/
    public static final String YM ="yyyy-MM";
    public static final String YM1 = "yyyy/MM";
    public static final String YM2 = "yyyy.MM";
    public static final String YMD = "yyyy-MM-dd";
    public static final String YMD1 = "yyyy/MM/dd";
    public static final String YMD2 = "yyyy.MM.dd";
    public static final String YMDHMS = "yyyy-MM-dd HH:mm:ss";
    public static final String YMDHMS1 = "yyyy/MM/dd HH:mm:ss";
    /**支持的时间字符串格式*/
    public static boolean patternSupport(String pattern){
        if(null!=pattern){
            pattern = pattern.trim();
            return pattern.equals(YM) || pattern.equals(YM1) || pattern.equals(YM2)
                    || pattern.equals(YMD) || pattern.equals(YMD1) || pattern.equals(YMD2)
                    || pattern.equals(YMDHMS) || pattern.equals(YMDHMS1);
        }
        return false;
    }

    /**LocalDateTime*/
    public LocalDateTime localDateTime(){
        return this.time;
    }
    /**LocalDate*/
    public LocalDate localDate(){
        return this.time.toLocalDate();
    }

    /**年操作*/
    public TenTime addYears(long year){
        time = time.plusYears(year);
        return this;
    }
    /**月操作*/
    public TenTime addMonths(long month){
        time = time.plusMonths(month);
        return this;
    }
    /**日操作*/
    public TenTime addDays(long day){
        time = time.plusDays(day);
        return this;
    }
    /**时操作*/
    public TenTime addHours(long hours){
        time = time.plusHours(hours);
        return this;
    }
    /**分操作*/
    public TenTime addMinutes(long minutes){
        time = time.plusMinutes(minutes);
        return this;
    }
    /**秒操作*/
    public TenTime addSeconds(long seconds){
        time = time.plusSeconds(seconds);
        return this;
    }

    /**跳到当前月第一天*/
    public TenTime jumpFirstDay(){
        time = time.with(TemporalAdjusters.firstDayOfMonth());
        return this;
    }
    /**跳到当前月最后一天*/
    public TenTime jumpLastDay(){
        time = time.with(TemporalAdjusters.lastDayOfMonth());
        return this;
    }
    /**跳到当前天零点*/
    public TenTime jumpFirstHMS(){
        time = LocalDateTime.of(time.toLocalDate(), LocalTime.MIN);
        return this;
    }
    /**跳到当前天最后一秒*/
    public TenTime jumpLastHMS(){
        time = LocalDateTime.of(time.toLocalDate(), LocalTime.MAX);
        return this;
    }

    /**当前是年第几天*/
    public int dayOfYear(){
        return time.getDayOfYear();
    }
    public static int dayOfYear(long timestamp){
        return new TenTime(timestamp).dayOfYear();
    }
    /**当前是月第几天*/
    public int dayOfMonth(){
        return time.getDayOfMonth();
    }
    public static int dayOfMonth(long timestamp){
        return new TenTime(timestamp).dayOfMonth();
    }
    /**当前是第几月*/
    public int monthOfYear(){
        return time.getMonthValue();
    }
    public static int monthOfYear(long timestamp){
        return new TenTime(timestamp).monthOfYear();
    }
    /**获取当月有多少天*/
    public int daysInMonth(){
        TenTime first = new TenTime(toTimeStamp()).jumpFirstDay();
        TenTime last  = new TenTime(toTimeStamp()).jumpLastDay();
        Duration duration = Duration.between(first.localDateTime(),last.localDateTime());
        return (int)duration.toDays()+1;
    }
    public static int daysInMonth(long timestamp){
        TenTime first = new TenTime(timestamp).jumpFirstDay();
        TenTime last  = new TenTime(timestamp).jumpLastDay();
        Duration duration = Duration.between(first.localDateTime(),last.localDateTime());
        return (int)duration.toDays()+1;
    }
    /**转换为10位毫秒数*/
    public int toTimeStamp(){
        return (int)time.toEpochSecond(chinaZone);
    }
    /**转换为日期*/
    public Date toDate(){
        Instant instant = time.toInstant(chinaZone);
        return Date.from(instant);
    }
    /**获取格式化字符串*/
    public String toDateStr(String pattern){
        if(toTimeStamp()==0){
            return "";
        }else{
            return time.format(DateTimeFormatter.ofPattern(pattern));
        }
    }
}
